﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Class UserPageViewModel : Inherits ObservableObject

    Private ReadOnly _userAvatarUrlBase As String = NavigationData.SERVER_ACCESS_URL + "/vdm/api/file/download/"
    Private ReadOnly _userAvatarDefaultUrl As String = "/VMDWebFrontend;component/Assets/SPGLP_icon.jpg"

    Private _user As User
    Public Property User As User
        Get
            Return _user
        End Get
        Set(value As User)
            SetProperty(_user, value)
        End Set
    End Property

    Private _userAvatar As BitmapImage
    Public Property UserAvatar As BitmapImage
        Get
            Return _userAvatar
        End Get
        Set(value As BitmapImage)
            SetProperty(_userAvatar, value)
        End Set
    End Property

    Private _ic_blog_items As List(Of UserBlogItem)
    Public Property IC_Blog_Items As List(Of UserBlogItem)
        Get
            Return _ic_blog_items
        End Get
        Set(value As List(Of UserBlogItem))
            SetProperty(_ic_blog_items, value)
        End Set
    End Property

    Public Property GoBackCommand As New RelayCommand(AddressOf GoBackClicked)

    Private ReadOnly _client As HttpClientBase

    Public Sub New()
        _client = New HttpClientBase()
    End Sub

    Public Sub SetUserId(userId As Integer)
        Call LoadUserAsync(userId)
    End Sub

    Private Async Function LoadUserAsync(userId As Integer) As Task
        Try
            Dim response = Await _client.GetAsync(Of User)(String.Format("/vdm/api/account/findUser/id/{0}", userId))
            If response.Code <> 200 Then
                MessageBox.Show("加载失败：" + response.Message)
                Return
            End If
            User = response.Data
            '用户的头像是否未设置，则加载默认头像
            If String.IsNullOrEmpty(User.Avatar) Then
                UserAvatar = New BitmapImage(New Uri(_userAvatarDefaultUrl, UriKind.Relative))
            Else
                UserAvatar = New BitmapImage(New Uri(_userAvatarUrlBase + User.Avatar, UriKind.Absolute))
            End If
            Await LoadICBlogItemsAsync()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("系统错误：" + ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
            Return
        End Try
    End Function

    Private Async Function LoadICBlogItemsAsync() As Task
        IC_Blog_Items = New List(Of UserBlogItem)
        Try
            Dim blogItems = New List(Of UserBlogItem)
            Dim response = Await _client.GetAsync(Of List(Of Blog))(String.Format("/vdm/api/blog/getBlog/uid/{0}", User.Id))
            If response.Code <> 200 Then
                MessageBox.Show("加载失败：" + response.Message)
                Return
            End If
            Dim blogs = response.Data
            For Each blog In blogs
                Dim blogItem As New UserBlogItem()
                blogItem.DataContext = blog
                blogItem.TB_Date.Text = blog.PostDate.ToString("yyyy-MM-dd")
                Await blogItem.LoadSongTitleAsync()
                blogItems.Add(blogItem)
            Next
            IC_Blog_Items = blogItems
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("系统错误：" + ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
            Return
        End Try
    End Function

    Private Sub GoBackClicked()
        NavigationService.Navigate(NavigationData.MAIN_LAYOUT_URI)
    End Sub

End Class
